package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Software;

import com.jgoodies.forms.layout.CellConstraints;

import controller.AdministradorController;
import controller.SoftwareController;

import gui.browse.Browse;
import gui.browse.BrowseListener;
import gui.util.GUIUtils;
import gui.util.JFrameCadastro;

public class SoftwareGUI extends JFrameCadastro{
	private static final long serialVersionUID = 1L;

	private JPanel panelCentral;
	public JButton botaoBrowse;
	public JTextField editCodigo;
	public JTextField editDescricao;
	public JTextField editVersao;
	public JCheckBox checkSistemaOperacional;
	
	public ActionListener actionIncluir;
	public ActionListener actionAlterar;
	public ActionListener actionExcluir;
	public ActionListener actionCancelar;
	public ActionListener actionGravar;

	public SoftwareGUI() {
		super();
		init();
	}

	public void init(){
		this.setTitle("Cadastro de Softwares");
		escreverRegistro(false);
	}

	public JPanel getPanelCentral() {
		if (panelCentral == null){
			panelCentral = new JPanel(GUIUtils.getLayout(6, 15));
			CellConstraints  cc = new CellConstraints();

			panelCentral.add(new JLabel("Codigo"),		cc.xywh(  2, 4,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditCodigo(),			cc.xywh( 10, 4,  5, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(getBotaoBrowse(),			cc.xywh( 16, 4,  2, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(new JLabel("Descricao"),	cc.xywh(  2, 6,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditDescricao(),		cc.xywh( 10, 6, 19, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
		
			panelCentral.add(new JLabel("Vers�o"),		cc.xywh(  2, 8,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditVersao(),			cc.xywh( 10, 8, 10, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(getCheckSistemaOperacional(),	cc.xywh( 10, 10, 10, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));

		}
		return panelCentral;
	}

	public JTextField getEditCodigo() {
		if (editCodigo == null){
			editCodigo = new JTextField();
			editCodigo.setEditable(false);
		}
		return editCodigo;
	}
	public JTextField getEditDescricao() {
		if (editDescricao == null){
			editDescricao = new JTextField();
		}
		return editDescricao;
	}

	public JTextField getEditVersao() {
		if (editVersao == null){
			editVersao = new JTextField();
		}
		return editVersao;
	}
	
	public JButton getBotaoBrowse(){
		return super.getBotaoBrowse(SoftwareController.getInstance());
	}

	public JCheckBox getCheckSistemaOperacional() {
		if (checkSistemaOperacional == null){
			checkSistemaOperacional = new JCheckBox("Sistema Operacional");
		}
		return checkSistemaOperacional;
	}
	
	@Override
	public ActionListener getActionIncluir() {
		if (actionIncluir == null){
			actionIncluir = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					limparDados();
					escreverRegistro(true);
					getEditCodigo().setText("0");
				}
			};
		}
		return actionIncluir;
	}
	
	@Override
	public ActionListener getActionAlterar() {
		if (actionAlterar == null){
			actionAlterar = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					escreverRegistro(true);
				}
			};
		}
		return actionAlterar;
	}

	@Override
	public ActionListener getActionExcluir() {
		if (actionExcluir == null){
			actionExcluir = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (getEditCodigo().getText() != null && !getEditCodigo().getText().trim().isEmpty()){
						try {
							int opcao = (JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluir o registro "+ getEditCodigo().getText() +"?",
									"T�tulo", JOptionPane.YES_NO_OPTION));
							if (opcao == JOptionPane.YES_OPTION) {
								SoftwareController.getInstance().excluirSoftware(Integer.valueOf(getEditCodigo().getText().trim()));
								limparDados();
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}
			};
		}
		return actionExcluir;
	}

	@Override
	public ActionListener getActionCancelar() {
		if (actionCancelar == null){
			actionCancelar = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					limparDados();
					escreverRegistro(false);
				}
			};
		}
		return actionCancelar;
	}

	@Override
	public ActionListener getActionGravar() {
		if (actionGravar == null){
			actionGravar = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (getEditCodigo().getText().trim() != null && !getEditCodigo().getText().trim().isEmpty()){
						if (getEditCodigo().getText().trim().equals("0")){
							try {
								SoftwareController.getInstance()
								.inserirSoftware(getEditDescricao().getText(), getEditVersao().getText(), getCheckSistemaOperacional().isSelected());
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
							try {
								SoftwareController.getInstance()
								.alterarSoftware(Integer.valueOf(getEditCodigo().getText().trim()), 
										getEditDescricao().getText(), getEditVersao().getText(), 
										getCheckSistemaOperacional().isSelected());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					escreverRegistro(false);
					limparDados();
					getBotaoFechar().grabFocus();
				}
			};
		}
		return actionGravar;
	}

	public void limparDados(){
		getEditCodigo().setText("");
		getEditDescricao().setText("");
		getEditVersao().setText("");
		getCheckSistemaOperacional().setSelected(false);
		
	}
	public void escreverRegistro(boolean b){
		getBotaoCancelar().setEnabled(b);
		getBotaoGravar().setEnabled(b);
		getBotaoIncluir().setEnabled(!b);
		getBotaoAlterar().setEnabled(!b);
		getBotaoExcluir().setEnabled(!b);
		getBotaoFechar().setEnabled(!b);
		getBotaoBrowse().setEnabled(!b);
		getEditCodigo().setEnabled(!b);
		getEditDescricao().setEnabled(b);
		getEditVersao().setEnabled(b);
		getCheckSistemaOperacional().setEnabled(b);
		getEditDescricao().grabFocus();
	}

	@Override
	public void preencherCampos(HashMap<String, Object> dados) {
		Software soft = null;
			try {
				soft = SoftwareController.getInstance().buscarSoftwarePorId((Integer) dados.get("codigo"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			editCodigo.setText(String.valueOf(soft.getId()));
			editDescricao.setText(soft.getDescricao());
			editVersao.setText(soft.getVersao());
			checkSistemaOperacional.setSelected(soft.isSistemaOperacional());

		
		
	}
}
